package com.example.myapplication4;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.app.AbilityContext;
import ohos.app.Context;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.distributed.common.UserInfo;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import ohos.rpc.*;
import ohos.utils.net.Uri;
import ohos.utils.zson.ZSONObject;

import java.util.HashMap;
import java.util.Map;



//此Service类用于处理用户的登录验证操作
public class DemoService extends Ability {
    private AbilityContext abilityContext;

    MyRemote myRemote = new MyRemote();
    //日志
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");


    @Override
    protected IRemoteObject onConnect(Intent intent) {

        //返回RemoteObject的对象
        return myRemote.asObject();
    }

    class MyRemote extends RemoteObject implements IRemoteBroker {
        private AbilityContext abilitycontext1;

        public MyRemote(String descriptor, AbilityContext abilitycontext1) {
            super(descriptor);
            this.abilitycontext1 = abilitycontext1;
        }

        public MyRemote() {
            super("descriptor");
        }

        //在此函数中进行js与java的连通
        @Override
        public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException {
            //根据不同的code值执行不同的操作
            switch (code) {
                case 150:
                    boolean islogined=LoginConfirm(data);
                    Map<String, Object> result2 = new HashMap<String, Object>();
                    if(islogined){
                        result2.put("ans", "已注册过");

                    }else{
                        result2.put("ans", "还没注册");
                    }
                    reply.writeString(ZSONObject.toZSONString(result2));
                    System.out.println(islogined);
                    break;

                default:
                    return false;
            }
            return true;
        }

        //处理登录验证操作的函数
        private boolean LoginConfirm(MessageParcel data) {
            DataAbilityHelper helper = DataAbilityHelper.creator(DemoService.this);
            String str = data.readString();
            UserInfol param = new UserInfol();
            try {
                //将传来的字符串JSON序列化
                param = ZSONObject.stringToClass(str, UserInfol.class);
            } catch (RuntimeException e) {
                System.out.println("failed");
            }
            //选定指定数据库的数据表
            Uri uri = Uri.parse("dataability:///com.example.myapplication4.UUDataAbility/users");
            //选定某一列数据
            String[] columns = {"userName"};
            DataAbilityPredicates dataAbilityPredicates = new DataAbilityPredicates();
            try {
                //返回查询结果集
                ResultSet rs = helper.query(uri, columns, dataAbilityPredicates);
                int rowCount = rs.getRowCount();
                if (rowCount > 0) {
                    rs.goToNextRow();
                    do {
                        String userName = rs.getString(0);
                        if(userName.equals(param.getUserName())){
                            return true;
                        }
                    } while (rs.goToNextRow());
                }
            } catch (DataAbilityRemoteException e) {
                e.printStackTrace();
            }
            return  false;
        }

        @Override
        public IRemoteObject asObject() {
            return this;
        }

    }
}